package com.sunfeng.n4.reentrant;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**  验证可冲入
 * @author sunfeng
 * @date 2021/03/30 13:30
 * <p>
 * 可中断
 * 可以设置超时时间
 * 可以设置为公平锁
 * 支持多个条件变量
 **/
public class ReentrantLockTest {
   private static ReentrantLock reentrantLock = new ReentrantLock();

    public static void main(String[] args) {


        reentrantLock.lock();
        try {
            method1();
        } finally {
            reentrantLock.unlock();
        }


    }

    public static void method1(){
        reentrantLock.lock();
        try {
            method2();
        } finally {
            reentrantLock.unlock();
        }
    }
    public static void method2(){

        System.out.println(reentrantLock.getHoldCount());
        reentrantLock.lock();
        try {
            System.out.println(reentrantLock.getHoldCount());
            System.out.println("aaaaaaa");
        } finally {
            reentrantLock.unlock();
        }
    }
}
